package com.hanlink.dsp.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.TextView;

import com.qztech.utils.YzDebug;

/**
 * Created by Administrator on 2018/9/5.
 */

public class XoverEditText extends androidx.appcompat.widget.AppCompatEditText implements TextView.OnEditorActionListener {
    public XoverEditText(Context context) {
        super(context);
        init(context, null);
    }

    public XoverEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public XoverEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);

    }
    private void init(Context context, AttributeSet attrs){
        setLongClickable(false);
        setOnEditorActionListener(this);
    }


    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        YzDebug.d("onKeyPreIme:"+ keyCode);
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            validEditTextValue();
        }
        return super.onKeyPreIme(keyCode, event);
    }

    public void validEditTextValue() {
        if (mValidEditTextListener != null) {
            mValidEditTextListener.onValidEditTextValue();
        }
        if (mValidEditTextListener2 != null){
            mValidEditTextListener2.onValidEditTextValue(this);
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        YzDebug.d("onEditorAction id:" +actionId);
        validEditTextValue();
        return false;
    }

    public interface ValidEditTextListener {
        void onValidEditTextValue();
    }

    private ValidEditTextListener mValidEditTextListener;

    public void setValidEditTextListener(ValidEditTextListener validEditTextListener) {
        this.mValidEditTextListener = validEditTextListener;
    }
//

    public interface ValidEditTextListener2 {
        void onValidEditTextValue(XoverEditText et);
    }

    private ValidEditTextListener2 mValidEditTextListener2;

    public void setValidEditTextListener(ValidEditTextListener2 validEditTextListener) {
        this.mValidEditTextListener2 = validEditTextListener;
    }

}
